<?php

class Adept_Template_Tag_Include extends Adept_Template_Tag 
{
    
    public function preParse($compiler) 
    {
        
    	if (!$this->hasAttribute('file')) {
    	    throw new Adept_Template_Exception("Attribute file not defined ", array(), $this->getLocation());
    	}
    	$file = $this->getAttributeValue('file');
    	
    	$locator = new Adept_Locator_Template();
    	try {
    	   $file = $locator->locate($file);
    	} catch (Adept_Exception $e) {
    	    throw new Adept_Template_Exception($e->getMessage(), array(), $this->getLocation());
    	}
    	
    	$source = file_get_contents($file);
    	
    	$parser = $compiler->createParser();
    	$builder = $compiler->createTreeBuilder();
    	
    	$parser->setObserver($builder);
    	$parser->setTemplateName($file);
    	$builder->setRoot($this);
    	$parser->parse($source);
    	
    	parent::preParse($compiler);
    }	
    
}
